package com.example.daxiguaforharmony.game.system;

import com.example.daxiguaforharmony.game.Fruits;
import com.example.daxiguaforharmony.game.FruitsEffect;
import com.example.daxiguaforharmony.game.GameData;
import com.example.daxiguaforharmony.game.GameScene;
import com.example.daxiguaforharmony.game.core.base.DisplayObject;
import com.example.daxiguaforharmony.game.core.featrue.IReactiveSystem;
import com.example.daxiguaforharmony.game.core.game.GameObject;

import java.util.ArrayList;
import java.util.List;

public class UseWoodenHammerSystem implements IReactiveSystem {
    private GameScene _scene = null;
    private List<Fruits> _randomList = new ArrayList<Fruits>();
    public boolean enabled = true;
    public UseWoodenHammerSystem(GameScene scene) {
        this._scene = scene;
    }

    @Override
    public boolean Filter(String type) {
        return this.enabled && type == GameScene.Event.USE_WOODEN_HAMMER_ITEM;
    }

    @Override
    public void Execute(String type, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
        this._randomList.clear();
        List<GameObject> c = this._scene.getGameObjects();
        for (int i = 0; i < c.size(); i++) {
            Fruits fruits = (Fruits)c.get(i);
            if (fruits == null || fruits.getTag() != 100) continue;
            if (fruits.getState() == 0) {
                this._randomList.add(fruits);
            }
        }
        if (this._randomList.size() <= 0) {
            String msg = "此道具，只能消除猕猴桃及以下水果！";
            this._scene.showToast(msg);
            return;
        }
        GameData gameData = this._scene.gameData;
        gameData.addItemNum(GameScene.ItemID.ITEM3, -1);
        this._scene.AddEvent(GameScene.Event.USE_ITEM, GameScene.ItemID.ITEM3, 1, null, null);
        int r = (int)(Math.random() * this._randomList.size());
        Fruits fruits = this._randomList.get(r);
        DisplayObject displayObject = fruits.getDisplayObject();
        float width = displayObject.width * displayObject.scaleX;
        FruitsEffect.playEffect(fruits.getFruitsId(), displayObject.x, displayObject.y, width, this._scene);
        this._scene.pushInFruitsPool(fruits);
        this._scene.playAudio(2);// hecheng
        this._randomList.clear();
    }

    @Override
    public void Destory() {}
}
